#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1010;
char a[N],b[N];
int dp[N][N];
int p[N][N];
char record[N];
int main()
{
    int n,m;
    scanf("%d %d",&n,&m);
    scanf("%s%s",a+1,b+1);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(a[i]==b[j]){
                dp[i][j]=dp[i-1][j-1]+1;
                p[i][j]=1;
            }else if(dp[i-1][j]>dp[i][j-1]){
                dp[i][j]=dp[i-1][j];
                p[i][j]=3;
            }else{
                dp[i][j]=dp[i][j-1];
                p[i][j]=2;
            }
        }
    }
    int k=dp[n][m];
    int i=n,j=m;
    while(i>0&&j>0)
    {
        if(a[i]==b[j]){
            record[k--]=a[i];
            --i;
            --j;
        }else if(p[i][j]==2){
            --j;
        }else{
            --i;
        }
    }
    cout << dp[n][m] << endl;
    for(int i=1;i<=dp[n][m];i++) cout << record[i] << " ";
    return 0;
}